home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MPW_TOOL
/
TOOLS
/
TOOLS_WI
/
ICON_8
/
TESTS
/
BTREES.ICN
< prev
next >
Wrap
Text File
|
1990-03-02
|
907b
|
44 lines
#
# B I N A R Y T R E E S
#
# This program accepts string representations of binary trees from
# standard input. It performs a tree walk and lists the leaves of
# each tree.
record node(data,ltree,rtree)
procedure main()
local line, tree
while line := read() do {
tree := tform(line)
write("tree walk")
every write(walk(tree))
write("leaves")
every write(leaves(tree))
}
end
procedure tform(s)
local value,left,right
if /s then return
s ? if value := tab(upto('(')) then {
move(1)
left := tab(bal(','))
move(1)
right := tab(bal(')'))
return node(value,tform(left),tform(right))
}
else return node(s)
end
procedure walk(t)
suspend walk(\t.ltree | \t.rtree)
return t.data
end
procedure leaves(t)
if not(\t.ltree | \t.rtree) then return t.data
suspend leaves(\t.ltree | \t.rtree)
end